package com.tjzx.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Property;

import com.tjzx.common.DocType;
import com.tjzx.common.NewsType;
import com.tjzx.common.Pager;
import com.tjzx.dao.EipNewsInfoDAO;
import com.tjzx.entity.EipNewsInfo;

public class EipNewsInfoService {
	private EipNewsInfoDAO eipNewsInfoDAO;

	public EipNewsInfoDAO getEipNewsInfoDAO() {
		return eipNewsInfoDAO;
	}

	public void setEipNewsInfoDAO(EipNewsInfoDAO eipNewsInfoDAO) {
		this.eipNewsInfoDAO = eipNewsInfoDAO;
	}
	
	public EipNewsInfo getEipNewsInfoById(Long id) {
		return eipNewsInfoDAO.findById(id);
	}
	
	public Pager<EipNewsInfo> getNews(int pageNo, int pageSize, int firstResult, int maxResults) {
		DetachedCriteria criteria = DetachedCriteria.forClass(EipNewsInfo.class);
		criteria.add(Property.forName("docType").eq(DocType.News.intValue()));
		criteria.add(Property.forName("isdel").isNull());
		
		int rowCount = eipNewsInfoDAO.getCount(criteria);
		criteria.addOrder(Property.forName("pubdate").desc());
		
		List list = eipNewsInfoDAO.findByCriteria(criteria, firstResult, maxResults);
		return new Pager<EipNewsInfo>(pageNo, pageSize, rowCount, list);
	}
	
	public Pager<EipNewsInfo> getHealthNews(int pageNo, int pageSize, int firstResult, int maxResults) {
		DetachedCriteria criteria = DetachedCriteria.forClass(EipNewsInfo.class);
		criteria.add(Property.forName("docType").eq(DocType.Others.intValue()));
		criteria.add(Property.forName("newsType").eq(NewsType.Health.intValue()));
		criteria.add(Property.forName("isdel").isNull());
		
		int rowCount = eipNewsInfoDAO.getCount(criteria);
		criteria.addOrder(Property.forName("pubdate").desc());
		
		List list = eipNewsInfoDAO.findByCriteria(criteria, firstResult, maxResults);
		return new Pager<EipNewsInfo>(pageNo, pageSize, rowCount, list);
	}
	
	public Pager<EipNewsInfo> getHealthManagerNews(int pageNo, int pageSize, int firstResult, int maxResults) {
		DetachedCriteria criteria = DetachedCriteria.forClass(EipNewsInfo.class);
		criteria.add(Property.forName("docType").eq(DocType.Others.intValue()));
		criteria.add(Property.forName("newsType").eq(NewsType.HealthManager.intValue()));
		criteria.add(Property.forName("isdel").isNull());

		int rowCount = eipNewsInfoDAO.getCount(criteria);
		criteria.addOrder(Property.forName("subject").desc());
		
		List list = eipNewsInfoDAO.findByCriteria(criteria, firstResult, maxResults);
		return new Pager<EipNewsInfo>(pageNo, pageSize, rowCount, list);
	}
	
	public Pager<EipNewsInfo> getTCNews(String subType, int pageNo, int pageSize, int firstResult, int maxResults) {
		DetachedCriteria criteria = DetachedCriteria.forClass(EipNewsInfo.class);
		if (subType.equals(DocType.Health.value())) {
			criteria.add(Property.forName("docType").eq(DocType.Health.intValue()));
		} else if(subType.equals(DocType.VIP.value())) {
			criteria.add(Property.forName("docType").eq(DocType.VIP.intValue()));
		} else if(subType.equals(DocType.Female.value())) {
			criteria.add(Property.forName("docType").eq(DocType.Female.intValue()));
		} else if(subType.equals(DocType.Entrance.value())) {
			criteria.add(Property.forName("docType").eq(DocType.Entrance.intValue()));
		}
		criteria.add(Property.forName("newsType").eq(NewsType.TCNews.intValue()));
		criteria.add(Property.forName("isdel").isNull());

		int rowCount = eipNewsInfoDAO.getCount(criteria);
		criteria.addOrder(Property.forName("subject").desc());
		
		List list = eipNewsInfoDAO.findByCriteria(criteria, firstResult, maxResults);
		return new Pager<EipNewsInfo>(pageNo, pageSize, rowCount, list);
	}
	public Pager<EipNewsInfo> getTCAll(String subType, int pageNo, int pageSize, int firstResult, int maxResults) {
		DetachedCriteria criteria = DetachedCriteria.forClass(EipNewsInfo.class);
		criteria.add(Property.forName("docType").eq(subType));
		criteria.add(Property.forName("newsType").eq(NewsType.TCNews.intValue()));
		criteria.add(Property.forName("isdel").isNull());

		int rowCount = eipNewsInfoDAO.getCount(criteria);
		criteria.addOrder(Property.forName("subject").desc());
		List list = eipNewsInfoDAO.findByCriteria(criteria, firstResult, maxResults);
		return new Pager<EipNewsInfo>(pageNo, pageSize, rowCount, list);
	}
	public Pager<EipNewsInfo> getTCFlow(int pageNo, int pageSize, int firstResult, int maxResults) {
		DetachedCriteria criteria = DetachedCriteria.forClass(EipNewsInfo.class);
		criteria.add(Property.forName("newsType").eq(NewsType.TCFlow.intValue()));
		criteria.add(Property.forName("isdel").isNull());

		int rowCount = eipNewsInfoDAO.getCount(criteria);
		criteria.addOrder(Property.forName("subject").desc());
		
		List list = eipNewsInfoDAO.findByCriteria(criteria, firstResult, maxResults);
		return new Pager<EipNewsInfo>(pageNo, pageSize, rowCount, list);
	}
	
	public Pager<EipNewsInfo> getTCNotes(int pageNo, int pageSize, int firstResult, int maxResults) {
		DetachedCriteria criteria = DetachedCriteria.forClass(EipNewsInfo.class);
		criteria.add(Property.forName("newsType").eq(NewsType.TCNotes.intValue()));
		criteria.add(Property.forName("isdel").isNull());

		int rowCount = eipNewsInfoDAO.getCount(criteria);
		criteria.addOrder(Property.forName("subject").desc());
		
		List list = eipNewsInfoDAO.findByCriteria(criteria, firstResult, maxResults);
		return new Pager<EipNewsInfo>(pageNo, pageSize, rowCount, list);
	}
	
	public Pager<EipNewsInfo> getPETCT(int pageNo, int pageSize, int firstResult, int maxResults) {
		DetachedCriteria criteria = DetachedCriteria.forClass(EipNewsInfo.class);
		criteria.add(Property.forName("newsType").eq(NewsType.PETCT.intValue()));
		criteria.add(Property.forName("isdel").isNull());

		int rowCount = eipNewsInfoDAO.getCount(criteria);
		criteria.addOrder(Property.forName("subject").desc());
		
		List list = eipNewsInfoDAO.findByCriteria(criteria, firstResult, maxResults);
		return new Pager<EipNewsInfo>(pageNo, pageSize, rowCount, list);
	}	
	
	public Pager<EipNewsInfo> getDeviceNews(int pageNo, int pageSize, int firstResult, int maxResults) {
		DetachedCriteria criteria = DetachedCriteria.forClass(EipNewsInfo.class);
		criteria.add(Property.forName("docType").eq(DocType.Device.intValue()));
		criteria.add(Property.forName("isdel").isNull());

		int rowCount = eipNewsInfoDAO.getCount(criteria);
		criteria.addOrder(Property.forName("edid").asc());
		
		List list = eipNewsInfoDAO.findByCriteria(criteria, firstResult, maxResults);
		return new Pager<EipNewsInfo>(pageNo, pageSize, rowCount, list);
	}

	public List<EipNewsInfo> getAll() {
		return eipNewsInfoDAO.findAll();
	}
	
	public void addVoteInfo(EipNewsInfo eipNewsInfo) {
		eipNewsInfoDAO.save(eipNewsInfo);
	}
	
	public void updateVoteInfo(EipNewsInfo eipNewsInfo) {
		eipNewsInfoDAO.merge(eipNewsInfo);
	}
	
	public void deleteVoteInfo(EipNewsInfo eipNewsInfo) {
		eipNewsInfoDAO.delete(eipNewsInfo);
	}
}
